<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
	Example from Nokia Maps API Playground, for more information visit http://api.maps.nokia.com
	or http://api.maps.nokia.com/2.1.0/playground/?example=routing
 -->
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9" />
		<title>Nokia Maps API Example: Create route from A to B</title>
		<!-- Styling for example container (NoteContainer & Logger)  -->
		<link rel="stylesheet" type="text/css" href="http://api.maps.nokia.com/2.1.0/playground/examples/exampleHelpers.css"/>
		<!-- By default we add ?kml=auto&heatmap=auto to auto load KML & heatmap package remove params if package are not needed -->
		<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.1.0/jsl.js?kml=auto&heatmap=auto"></script>
		<!-- JavaScript for example container (NoteContainer & Logger)  -->
		<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.1.0/playground/examples/exampleHelpers.js"></script>
		<style type="text/css">
			html {
				overflow:hidden;
			}

			body {
				margin: 0;
				padding: 0;
				overflow: hidden;
				width: 100%;
				height: 100%;
				position: absolute;
			}

			#mapContainer {
				width: 100%;
				height: 100%;
				left: 0;
				top: 0;
				position: absolute;
			}
		</style>
	</head>
	<body>
		<div id="mapContainer"></div>
		<script type="text/javascript">
			// Don't forget to set your API credentials
			// nokia.maps.util.ApplicationContext.set({"appId": "YOUR APPID", "authenticationToken": "YOUR TOKEN"});
		</script>
		<div id="uiContainer"></div>
		<script>
var route_path = '<?php echo $route ?>';
// Get the DOM node to which we will append the map
var mapContainer = document.getElementById("mapContainer");

/* We create an UI notecontainer for example description
 * NoteContainer is an UI helper function and not part of the Nokia Maps API
 * See exampleHelpers.js for implementation details
 */
var noteContainer = new NoteContainer({
	id: "routingUi",
	parent: document.getElementById("uiContainer"),
	title: "<?php if($journeyname==""){ echo "New Map";}else{echo $journeyname;} ?>",
	content:
		'<a href="/journeys/jlist">Back to routes</a><br />Approx Distance:<span id="approxDist"></span>'
});
// Create a map inside the map container DOM node
var map = new nokia.maps.map.Display(mapContainer, {
		// Initial center and zoom level of the map
		center: [52.51, 13.4],
		zoomLevel: 7,
		// We add the behavior component to allow panning / zooming of the map
		components:[new nokia.maps.map.component.Behavior()]
	}),
	router = new nokia.maps.routing.Manager(); // create a route manager;

// The function onRouteCalculated  will be called when a route was calculated
var onRouteCalculated = function (observedRouter, key, value) {
		if (value == "finished") {
			var routes = observedRouter.getRoutes();

			//create the default map representation of a route
			var mapRoute = new nokia.maps.routing.component.RouteResultSet(routes[0]).container;
			map.objects.add(mapRoute);

			//Zoom to the bounding box of the route
			map.zoomTo(mapRoute.getBoundingBox(), false, "default");
		} else if (value == "failed") {
			alert("The routing request failed.");
		}
	};

/* We create on observer on router's "state" property so the above created
 * onRouteCalculated we be called once the route is calculated
 */
router.addObserver("state", onRouteCalculated);

// Create waypoints
var waypoints = new nokia.maps.routing.WaypointParameterList();

rp = route_path.split(';;');


	//Setting Up the points and paths :)
	distance = 0;
	for(var j=0;j<rp.length;j++){

		if(!rp[j])
			break;
		rp[j] = rp[j].split(';');

		if(!rp[j][0])
			break;

		rp[j][0] = rp[j][0].split('~');
		rp[j][1] = rp[j][1].split('~');

		var la=rp[j][0][1];
		var lo=rp[j][1][1];
		map.set("center", new nokia.maps.geo.Coordinate(parseFloat(la),parseFloat(lo)));
		waypoints.addCoordinate(new nokia.maps.geo.Coordinate(parseFloat(la), parseFloat(lo)));
		try{
				distance += waypoints.coords[waypoints.coords.length-1].distance(waypoints.coords[waypoints.coords.length-2]);
		}catch(e){}
	}
	if(distance>0){
		distance = parseInt(distance / 1000);
	}
	document.getElementById("approxDist").innerHTML = distance.toString() + " KM";
	loadingflag=false;



/* Properties such as type, transportModes, options, trafficMode can be
 * specified as second parameter in performing the routing request.
 *
 * See for the mode options the "nokia.maps.routing.Mode" section in the developer's guide
 */
var modes = [{
	type: "shortest",
	transportModes: ["car"],
	options: "avoidTollroad",
	trafficMode: "default"
}];

// Calculate the route (and call onRouteCalculated afterwards)
router.calculateRoute(waypoints, modes);



 </script>
	</body>
</html>
